public without sharing class AccountsReceivableStatementHandler {
    
    public static void gatherData(List<AccountsReceivableStatement__c> datas){
    	///应收绩效的Id
    	Set<Id> apSet = new Set<Id>();

    	for (AccountsReceivableStatement__c ars :datas ) {
    		apSet.add(ars.AcceptancePerformance__c);
    	}


    	///查询应收绩效的所有人
    	Map<Id,User>  apOwnerMap= new Map<Id,User>();
    	Map<Id,Boolean> userMap= new Map<Id,Boolean>();
    	for(AcceptancePerformance__c ap:[select Id,OwnerId ,Owner.Profile.Name from AcceptancePerformance__c where Id in:apSet and Owner.isActive = true ]){
    		User u = new User(Id=ap.OwnerId);

    		apOwnerMap.put(ap.Id,u);
    		userMap.put(ap.OwnerId,ap.Owner.Profile.Name.contains('销售员'));
    	}


    	//销售部直销销售员
    	//销售部分销销售员
    	//
    	Map<Id,AcceptancePerformance__c> apMap = new Map<Id,AcceptancePerformance__c>();
    	///找出所有的应收账款账龄
    	for (AccountsReceivableStatement__c ars:[select Id,AcceptancePerformance__c,Overdue90And180Deductions__c,Overdue90And180DeductionsAdjust__c,Overdue90And180DeductionsManager__c,Overdue90And180DeductionsManagerAdjust__c,AccountDeductionInThisQuarter__c,AccountDeductionInThisQuarterAdjust__c,AccountDeductionInManager__c,AccountDeductionInManagerAdjust__c from AccountsReceivableStatement__c where AcceptancePerformance__c in:apSet] ) {
    		///存在这个User
    		if(apOwnerMap.containsKey(ars.AcceptancePerformance__c)){
    			User u = apOwnerMap.get(ars.AcceptancePerformance__c);
    			if(apMap.containsKey(ars.AcceptancePerformance__c )){

	    			AcceptancePerformance__c ap  = apMap.get(ars.AcceptancePerformance__c);
	    			if(userMap.get(u.Id)){
						if(ars.Overdue90And180Deductions__c != null )ap.Overdue90And180Deductions__c += ars.Overdue90And180Deductions__c;
                        if(ars.Overdue90And180DeductionsAdjust__c != null )ap.Overdue90And180DeductionsAdjust__c += ars.Overdue90And180DeductionsAdjust__c;
                        if(ars.AccountDeductionInThisQuarterAdjust__c != null )ap.AccountDeductionInThisQuarterAdjust__c += ars.AccountDeductionInThisQuarterAdjust__c;
	                    if(ars.AccountDeductionInThisQuarter__c != null )ap.AccountDeductionInThisQuarter__c += ars.AccountDeductionInThisQuarter__c;
	    			}else{
	    				if(ars.Overdue90And180DeductionsManager__c != null )ap.Overdue90And180Deductions__c += ars.Overdue90And180DeductionsManager__c;
                        if(ars.Overdue90And180DeductionsManagerAdjust__c != null )ap.Overdue90And180DeductionsAdjust__c += ars.Overdue90And180DeductionsManagerAdjust__c;
	    				if(ars.AccountDeductionInManager__c != null )ap.AccountDeductionInThisQuarter__c += ars.AccountDeductionInManager__c;
                        if(ars.AccountDeductionInManagerAdjust__c != null )ap.AccountDeductionInThisQuarterAdjust__c += ars.AccountDeductionInManagerAdjust__c;
	    			}
	    			
	    			apMap.put(ap.Id, ap);
	    		}else{
	    			AcceptancePerformance__c ap = new AcceptancePerformance__c();
	    			ap.Id = ars.AcceptancePerformance__c;
    				if(userMap.get(u.Id)){
    					ap.AccountDeductionInThisQuarter__c = ars.AccountDeductionInThisQuarter__c == null ? 0 : ars.AccountDeductionInThisQuarter__c ;
	    				ap.AccountDeductionInThisQuarterAdjust__c = ars.AccountDeductionInThisQuarterAdjust__c == null ? 0 : ars.AccountDeductionInThisQuarterAdjust__c ;
                        ap.Overdue90And180Deductions__c = ars.Overdue90And180Deductions__c == null ? 0 :ars.Overdue90And180Deductions__c ;
                        ap.Overdue90And180DeductionsAdjust__c = ars.Overdue90And180DeductionsAdjust__c == null ? 0 :ars.Overdue90And180DeductionsAdjust__c ;
    				}else{
    					ap.AccountDeductionInThisQuarter__c = ars.AccountDeductionInManager__c == null ? 0 : ars.AccountDeductionInManager__c ;
	    				ap.Overdue90And180Deductions__c = ars.Overdue90And180DeductionsManager__c == null ? 0 :ars.Overdue90And180DeductionsManager__c ;
                        ap.AccountDeductionInThisQuarterAdjust__c = ars.AccountDeductionInManagerAdjust__c == null ? 0 : ars.AccountDeductionInManagerAdjust__c ;
                        ap.Overdue90And180DeductionsAdjust__c = ars.Overdue90And180DeductionsManagerAdjust__c == null ? 0 :ars.Overdue90And180DeductionsManagerAdjust__c ;
    				}

	    			
	    			apMap.put(ap.Id, ap);

	    		}

    		}
    		
    	}

    	if(apMap.values().size() > 0) update apMap.values();




    }
}